package com.book.config;

import com.book.interceptor.LoginInterceptor;
import com.book.interceptor.PermissionInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author SuperYu  2024/8/8
 */
// @Configuration
public class MyMVCConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .allowedHeaders("*")
                .exposedHeaders("*")
                .maxAge(3600);
    }

    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    public PermissionInterceptor permissionInterceptor() {
        return new PermissionInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] path = {
                "/api/login",
                "/index",
                "/error",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/swagger-resources/**",
                "/favicon.ico"

        };

        registry.addInterceptor(this.loginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(path);

        registry.addInterceptor(this.permissionInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(path);

    }
}
